package com.alan.quartz.dao.impl;

import com.alan.quartz.dao.TaskDao;
import com.alan.quartz.model.SysTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by liufan
 * 2018-12-12
 */
@Repository
public class TaskDaoImpl implements TaskDao{

    @Autowired
    JdbcTemplate jdbcTemplate;


    @Override
    public List<SysTask> selectTask() {
        String sql = "SELECT id ,job_name as jobName,job_group AS jobGroup ,job_status AS  jobStatus,job_class AS jobClass,cron_expression AS cronExpression,job_description AS jobDescription,status,create_by AS createBy,create_date AS createDate,update_by AS updateBy,update_date AS updateDate FROM sys_task where status = '0'";

        return jdbcTemplate.query(sql,new RowMapper<SysTask>() {
            @Override
            public SysTask mapRow(ResultSet arg0, int arg1) throws SQLException {
                SysTask t = new SysTask();
                t.setId(arg0.getLong("id"));
                t.setJobName(arg0.getString("jobName"));
                t.setCreateBy(arg0.getString("createBy"));
                t.setCreateDate(arg0.getDate("createDate"));
                t.setCronExpression(arg0.getString("cronExpression"));
                t.setJobClass(arg0.getString("jobClass"));
                t.setJobDescription(arg0.getString("jobDescription"));
                t.setJobGroup(arg0.getString("jobGroup"));
                t.setJobStatus(arg0.getString("jobStatus"));
                t.setStatus(arg0.getString("status"));
                t.setUpdateby(arg0.getString("updateby"));
                t.setUpdateDate(arg0.getDate("updateDate"));
                return t;
            }
        });
    }


}
